<template>

    <div class="loginBox">
      <el-form label-position="left" :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px" class="demo-ruleForm loginBox-inner">
        <h1 class="loginBox-title1">居民健康管理系统</h1>

        <el-form-item prop="account">
          <el-input type="text" v-model="loginForm.account" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码">
            <svg-icon slot="prefix" icon-class="password" />
          </el-input>
        </el-form-item>


        <el-form-item style="width:100%;">
          <el-button type="primary" style="width:100%;" @click="submitForm" :loading="loading" >登录</el-button>
        </el-form-item>


        <el-form-item style="width:100%">
          <div style="float: right;">
            <el-link @click.native="register" type="primary" :underline="false">
              <span>[ 注册账号 ]</span>
            </el-link>
          </div>
        </el-form-item>

      </el-form>
    </div>

</template>

<script>

export default {
  name: 'Login',
  data () {
    return {
      loading: false,
      redirect: undefined,
      loginForm: {
        account: '',
        password: '',
        token: ''
      },
      codeUrl: "",
      loginRules: {
        account: [{required: true, message: '请输入账号', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}]
      },
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    register() {
      this.$router.push({ path: '/register' });
    },
    submitForm () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
              .then(() => {
                this.$router.push({ path: '/'})
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
        }
      });
    }
  }
}
</script>

<style>

.loginBox {

   box-sizing: border-box;
   width: 100%;
   height: 100%;
   padding-top: 10%;
   background-image:url("../../assets/images/bg.jpg");
   background-repeat: no-repeat;
   background-position: center right;
   background-size: 100%;
}

.loginBox-inner {
  border-radius: 10px;
  margin: 100px auto;
  width: 400px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.loginBox-title2 {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
  font-size: 20px;
}

.loginBox-title1 {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
  font-size: 28px;
}

.login-code img {
  width: 100%;
  cursor:pointer;
  vertical-align:middle
}
</style>
